// pages/paymentOrder/index.js
const {
  post,
  get,
  globalData
} = getApp();
Page({
  data: {
    show: false,
    active: 0,
    memberAddress: {},
    productInfoList: [],
    productInfoListNum: 0,
    data: {
      discoutNum: 0,
      totalMoney: 0,
      dlyPrice: 0,
      goodsAmount: 0,
      couponPrice: 0
    },
    params: {
      unique_id: 0,
      num: 0,
      coupon_id: "",
      share_member: ""
    },
    usableList: [],
    disableList: [],
    type: 0, //0是单独购买1是购物车
    showPage: false
  },
  //  初始化加载
  onLoad: function (e) {
    if (e.hasOwnProperty("unique_id")) {
      this.setData({
        "params.unique_id": e.unique_id,
        "params.num": e.value,
        "type": 0,
        "params.share_member": globalData.share_member
      });
    } else {
      this.setData({
        "type": 1,
        "params.share_member": globalData.share_member
      });
    };
  },
  // 获取购买数据
  getProductList() {
    post("/order/inmediately", this.data.params).then(res => {
      if (res.result == 1) {
        this.setData({
          "memberAddress": res.data.memberAddress,
          "productInfoList": res.data.productInfoList,
          "data.discoutNum": res.data.discoutNum,
          "data.totalMoney": res.data.totalMoney,
          "data.dlyPrice": res.data.dlyPrice,
          "data.goodsAmount": res.data.goodsAmount,
          "showPage": true,
          "productInfoListNum": res.data.productInfoList.reduce((a, b) => {
            return a + b.num;
          }, 0)
        })
      } else {
        wx.showModal({
          content: `${res.message}`,
          showCancel: false,
          confirmText: "返回",
          success(res) {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },
  // 购物车数据
  getCartList() {
    post("/order/fromcart").then(res => {
      if (res.result == 1) {
        this.setData({
          "memberAddress": res.data.memberAddress,
          "productInfoList": res.data.productInfoList,
          "data.discoutNum": res.data.discoutNum,
          "data.totalMoney": res.data.totalMoney,
          "data.dlyPrice": res.data.dlyPrice,
          "data.goodsAmount": res.data.goodsAmount,
          "showPage": true,
          "productInfoListNum": res.data.productInfoList.reduce((a, b) => {
            return a + b.num;
          }, 0)
        })
      } else {
        wx.showModal({
          content: `${res.message}`,
          showCancel: false,
          confirmText: "返回",
          success(res) {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },
  // 打开优惠券列表
  showCoupon() {
    if (this.data.type == 0) {
      post("/coupon/immediately", {
        unique_id: this.data.params.unique_id
      }).then(res => {
        if (res.result == 1) {
          this.setData({
            usableList: res.data.usableList,
            disableList: res.data.disableList,
            show: true,
            active: 0
          })
        }
      })
    } else {
      post("/coupon/fromcart").then(res => {
        if (res.result == 1) {
          this.setData({
            usableList: res.data.usableList,
            disableList: res.data.disableList,
            show: true,
            active: 0
          })
        }
      })
    }
  },
  // 优惠券切换
  onChange(e) {
    this.setData({
      active: e.detail.index
    })
  },
  // 改变优惠券
  changeCoupon: function (e) {
    if (this.data.params.coupon_id == e.currentTarget.dataset.coupon_id) {
      this.setData({
        "params.coupon_id": "",
        "data.couponPrice": 0,
        "show": false
      })
    } else {
      this.setData({
        "params.coupon_id": e.currentTarget.dataset.coupon_id,
        "data.couponPrice": e.currentTarget.dataset.amount,
        "show": false
      })
    }
  },
  // 提交订单
  onClickButton() {
    if (this.data.memberAddress == null) {
      wx.showToast({
        title: '请添加收货地址',
        icon: 'none',
        duration: 2000
      });
      return;
    };
    if (this.data.type == 0) {
      post("/order/create-inmediately", {
        ...this.data.params,
        addr_id: this.data.memberAddress.id,
        remark: ""
      }).then(res => {
        if (res.result == 1) {
          get("/order/pay", {
            order_sn: res.data.sn
          }).then(res1 => {
            if (res1.result == 1) {
              wx.requestPayment({
                timeStamp: res1.data.timeStamp,
                nonceStr: res1.data.nonceStr,
                package: res1.data.packageValue,
                signType: res1.data.signType,
                paySign: res1.data.paySign,
                success(res2) {
                  wx.navigateTo({
                    url: "../order/index?active=0",
                  })
                },
                fail(res2) {
                  wx.navigateTo({
                    url: `../myOrderDetail/index?sn=${res.data.sn}`,
                  })
                }
              })
            }
          })
        }
      });
    } else {
      post("/order/create-fromcart", {
        coupon_id: this.data.params.coupon_id,
        addr_id: this.data.memberAddress.id,
        share_member: this.data.params.share_member,
        remark: ""
      }).then(res => {
        if (res.result == 1) {
          get("/order/pay", {
            order_sn: res.data.sn
          }).then(res1 => {
            if (res1.result == 1) {
              wx.requestPayment({
                timeStamp: res1.data.timeStamp,
                nonceStr: res1.data.nonceStr,
                package: res1.data.packageValue,
                signType: res1.data.signType,
                paySign: res1.data.paySign,
                success(res2) {
                  wx.navigateTo({
                    url: "../order/index?active=0",
                  })
                },
                fail(res2) {
                  wx.navigateTo({
                    url: `../myOrderDetail/index?sn=${res.data.sn}`,
                  })
                }
              })
            }
          })
        }
      });
    }
  },
  // 不使用优惠券
  noUseCoupon() {
    this.setData({
      "params.coupon_id": "",
      "data.couponPrice": 0,
      "show": false
    })
  },
  // 关闭优惠券
  onClose() {
    this.setData({
      show: false
    });
  },
  // 跳转地址列表
  goToAddress() {
    wx.navigateTo({
      url: "../addressList/index",
    })
  },
  // 返回过来显示
  onShow: function (e) {
    if (this.data.type == 0) {
      this.getProductList();
    } else {
      this.getCartList();
    };
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  }
})